import React, { useState, useEffect } from 'react';
import { InfoList, is_empty, cline, uDb, IkFind, ck_empty, obj_str, obj_int, IkSelect, useForm, NuiDlg, IkInput } from 'sui';
import { get_tp } from '../stock/cmd';

const ck_int = (v, pre) => {
    if (!v) {
        return pre + "不能为空";
    }

    const tmp = parseInt(v, 10);
    if (isNaN(tmp)) {
        return pre + "必须为数字";
    }

    if (tmp <= 0) {
        return pre + "必须大于0";
    }
};

const DlgInfo = ({ open, cur, setOpen, hcmd }) => {
    const [value, setValue] = useState();
    const form = useForm(setValue);

    const hdone = () => {
        const ck = {
            box: (v) => {
                const e = ck_int(v, "盒数");
                if (e) {
                    return e;
                }

                const box = parseInt(v, 10);
                const old = obj_int(cur, "box");

                if (old !== box) {
                    return "盒数不同";
                }

                return null;
            }
        };

        if (!form.hcheck(ck, value)) {
            return;
        }

        setOpen(false);
        hcmd();
    };

    const getData = () => {
        if (!cur) {
            return null;
        }

        return [
            { des: "总数量", value: obj_str(cur, "num") },
            { des: "单盒数量", value: obj_str(cur, "snum") },
            { des: "盒数", value: obj_str(cur, "box") },
        ];
    };

    return (
        <NuiDlg open={open} title="确认" hcmd={hdone} hcancel={() => setOpen(false)}>
            <InfoList data={getData()}/>
            <div className="text-2xl text-blue-500">您要打印的张数为
            <span className="mx-2 text-red-500 font-bold text-4xl">{obj_str(cur, "box")}</span>
            请再次确认要打印的张数</div>
            <IkInput label="盒数" value={value} form={form} name="box" className="ml-2"/>
        </NuiDlg>
    );
};

const DlgVal = ({ open, cur, setOpen, hcmd }) => {
    const [main, setMain] = useState();
    const [open_info, openInfo] = useState(false);
    const [value, setValue] = useState();
    const form = useForm(setValue);

    useEffect(() => {
        if (!open) {
            return;
        }

        setValue(null);
    }, [open]);

    const hdone = () => {
        const ck = {
            level: (v) => ck_empty(v, "品质"),
            snum: (v) => ck_int(v, "单盒数量"),
            num: (v) => {
                const e = ck_int(v, "总数量");
                if (e) {
                    return e;
                }

                const tmp = parseInt(v, 10);

                if (tmp > obj_int(cur, "num")) {
                    return "数量不能超出总数量";
                }

                return null;
            },
            box: (v) => {
                const e = ck_int(v, "盒数");
                if (e) {
                    return e;
                }
                const tmp = parseInt(v, 10);

                if (tmp > 30) {
                    return "一次最多打印30张";
                }

                return null;
            },
        };

        if (!form.hcheck(ck, value)) {
            return;
        }

        const box = obj_int(value, "box");

        const cmd = {
            snum: obj_int(value, "snum"),
            num: obj_int(value, "num"),
            box: box,
            level: obj_str(value, "level"),
        };

        setMain(cmd);
        openInfo(true);
    };

    const hok = () => {
        form.hclear();
        setOpen(false);

        hcmd(main);
    };

    const lst = [
        {id: "a", des: "A优等品"},
        {id: "b", des: "B次等品"},
    ];

    const hcancel = () => {
        form.hclear();
        setOpen(false);
    };

    return (
        <NuiDlg open={open} title="合格数量" hcmd={hdone} hcancel={hcancel}>
            <div className="flex flex-col">
                <IkInput label="总数量" value={value} form={form} name="num" className="ml-2"/>
                <IkInput label="单盒数量" value={value} form={form} name="snum" className="ml-2"/>
                <IkInput label="盒数" value={value} form={form} name="box" className="ml-2"/>
                <IkSelect label="品质" value={value} form={form} name="level" lst={lst}/>
            </div>
            <DlgInfo open={open_info} setOpen={openInfo} cur={main} hcmd={hok}/>
        </NuiDlg>
    );
};

const DlgNg = ({ open, cur, setOpen, mod, hcmd, ng }) => {
    const [lst_ng, lstNg] = useState();
    const [value, setValue] = useState();
    const form = useForm(setValue);

    useEffect(() => {
        if (!open) {
            return;
        }

        const tp = get_tp(mod);

        uDb(null, "file/get/ngp",
            {
                line: cline(),
                sn: tp
            },
            r => {
                const user = r.user;
                if (is_empty(user)) {
                    lstNg(null);
                    return;
                }

                const tmp = [];

                user.forEach(d => tmp.push({
                    id: d,
                    sn: d,
                    name: ng[d],
                }));

                lstNg(tmp);
            }, e => lstNg(null));
        setValue(null);
    }, [open, mod, ng]);

    const ck_int = (v, pre) => {
        if (!v) {
            return pre + "不能为空";
        }

        const tmp = parseInt(v, 10);
        if (isNaN(tmp)) {
            return pre + "必须为数字";
        }

        if (tmp <= 0) {
            return pre + "必须大于0";
        }
    };

    const hdone = () => {
        const ck = {
            ngson: (v) => ck_empty(v, "不合格原因"),
            num: (v) => {
                const e = ck_int(v, "总数量");
                if (e) {
                    return e;
                }

                const tmp = parseInt(v, 10);

                if (tmp > obj_int(cur, "num")) {
                    return "数量不能超出总数量";
                }

                return null;
            }
        };

        if (!form.hcheck(ck, value)) {
            return;
        }

        form.hclear();
        setOpen(false);

        hcmd({
            num: obj_int(value, "num"),
            ngson: obj_str(value, "ngson"),
        });
    };

    const hcancel = () => {
        form.hclear();
        setOpen(false);
    };

    return (
        <NuiDlg open={open} title="合格数量" hcmd={hdone} hcancel={hcancel}>
            <div className="flex flex-col">
                <IkInput label="数量" value={value} form={form} name="num" className="ml-2"/>
                <IkFind label="不合格原因" value={value} form={form} name="ngson" className="ml-2" lst={lst_ng}/>
            </div>
        </NuiDlg>
    );
};

export {DlgNg, DlgVal};
